<?php
//1.定义图片的标准类型
//用于判断遍历文件夹下文件时是否为图片
$imgTypes = ["jpg","jpeg","png","gif"];
//1.定义空数组用于存储图片路径值
//2.将用于选择头像图片时的遍历展示
$imgArr = [];
//遍历attachment文件夹的所有文件
foreach(glob("./attachment/*") as $v):
    //获取文件的后缀名
    $fileType = strtolower(ltrim(strrchr($v,"."),"."));
    //如果后缀名不属性图片类型则属性文件夹需要再往下遍历
    if(!in_array($fileType,$imgTypes)){
        foreach (glob($v."/*") as $v2){
            //将图片路径存到空数组中
            $imgArr[] = $v2;
        };
    }else{
        //将图片路径存到空数组中
        $imgArr[] = $v;
    }
endforeach;
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>修改学生信息</title>
    <script type="application/javascript" src="./static/jquery-1.11.3.js"></script>
</head>
<style>
    .avatorBox.cur{
        outline:2px solid red;
        outline-offset:-5px;
    }
</style>
<body>
	<?php include './view/header.php' ?>
    <div class="container" style="margin-bottom: 500px">
        <div class="row">
			<?php include './view/left.php' ?>
            <div class="col-lg-9">
                <!-- TAB NAVIGATION -->
                <ul class="nav nav-tabs" role="tablist">
                    <li><a href="<?php echo u('lists');?>" role="tab" data-toggle="tab">学生列表</a></li>
                    <li class="active"><a role="tab" data-toggle="tab">编辑</a>
                    </li>
                </ul>
                <form action="" method="post" role="form" style="margin-top: 20px;" enctype="multipart/form-data">
                    <div class="form-group">
                        <select name="gid" class="form-control" required>
                            <!--遍历显示班级名称的数据；用于用户选择学生相应的班级-->
                            <option value="<?php echo $gradeData[0]['gid'];?>">请选择班级</option>
                                <!--“请选择班级”为提示信息，如果用户没有选择班级，为防出错因此设置一个gid值给该选项-->
                                <?php foreach ($gradeData as $v):?>
                                    <option value="<?php echo $v['gid'];?>" <?php if($v['gid']==$data['gid'])echo 'selected';?>>
                                        <?php echo $v['gname'];?>
                                    </option>
							    <?php endforeach;?>
                        </select>
                    </div>
                    <!--以下调用显示修改前的学生数据-->
                    <div class="form-group">
                        <input type="text" class="form-control" name="sname" id="" placeholder="name"
                               value="<?php echo $data['sname']?>" required>
                    </div>
                    <div class="form-group">
                        <input type="file" name="profileUpload" class="form-control" value="<?php echo $data['profile'];?>">
                        <a href="javascript:;" class="btn btn-xs btn-info" style="margin-top: 10px;" id="showAttachment">显示素材</a>
                        <div style="display: none;flex-wrap: wrap;margin-top: 20px" id="attachmentBox">
                            <!--遍历显示图片的数据供用户选择-->
                            <?php foreach ($imgArr as $v):?>
                                <a style="cursor: pointer" class="avatorBox"><img src="<?php echo $v?>" style="width: 80px;height: 80px;margin: 10px"></a>
                            <?php endforeach;?>
                        </div>
                        <input type="hidden" value="<?php echo $data['profile']?>" name="avator">

                    </div>
                    <div class="form-group">
                        <div class="radio ">
                            <label class="radio-inline">
                                <input type="radio" name="sex" value="男" <?php if($data['sex']=='男')echo 'checked';?>>
                                男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="sex" value="女" <?php if($data['sex']=='女')echo 'checked';?>>
                                女
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="date" name="birthday" class="form-control" value="<?php echo $data['birthday']?>" required>
                    </div>
                    <div class="form-group">
                        <textarea name="introduction" placeholder="introduce yourself" cols="30" rows="4" class="form-control"><?php echo $data['introduction']?></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
            </div>

        </div>

    </div>
	<?php include './view/footer.php' ?>


    </body>
<script>
    //刚载入页面时默认选中最后一个
    $('.avatorBox').last().addClass('cur');
    //获得最后一个图片的src值
    var src = $('.avatorBox').eq(0).find('img').attr('src');
    //将最后一个图片的src值赋值给隐藏域
    $('input[name=avator]').val(src);

    //当点击图片时
    $('.avatorBox').click(function () {
        //显示选中效果（红框）
        $(this).addClass('cur').siblings('.avatorBox').removeClass('cur');
        //获得选中的图片的src值
        var src = $(this).find('img').attr('src');
        //将选中的图片的src值赋值给隐藏域
        $('input[name=avator]').val(src);
    })

    //“显示素材”/“隐藏素材”点击显/隐素材图片的效果
    var a=-1;//定义状态变量
    $('#showAttachment').click(function () {
        a=-1*a;
        if(a==1){
            //显示素材图
            $(this).html("隐藏素材");
            $('#attachmentBox').show(1000).css('display','flex');
        }else {
            //隐藏素材图上
            $(this).html("显示素材");
            $('#attachmentBox').hide(1000);
        }
    })
</script>




</html>